<template>
  <div>
    <el-form
      :inline="true"
      class="demo-form-inline"
      style="width: 50%; text-align: left; margin-left: 330px"
    >
      <el-form-item>
        <el-input placeholder="请输入联系人名字" v-model="keyWord"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search()" round>查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="addClient()" round
          >增加联系人</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      :data="ClientList"
      tooltip-effect="dark"
      style="width: 60%; margin: 0px auto"
    >
      <!-- 多选框列 
     prop:指定该列要显示的数据属性
     -->
      <el-table-column type="selection" width="55" prop="cli_id" v-if="false">
      </el-table-column>

      <el-table-column prop="cli_name" label="姓名" width="120">
      </el-table-column>
      <el-table-column prop="cli_tel" label="电话" width="120">
      </el-table-column>
      <el-table-column prop="cli_job" label="职务" width="120">
      </el-table-column>
      <el-table-column label="性别" width="120">
        <div slot-scope="scope">
          <span v-if="scope.row.cli_sex == 1">女</span>
          <span v-else>男</span>
        </div>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            round
            @click="updCliInfo(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            round
            @click="delClient(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 修改模态框开始 -->
    <el-dialog title="编辑联系人信息" :visible.sync="ClientInfoDialog">
      <el-form
        inline="true"
        :rules="rules"
        ref="cliForm"
        :model="updClientInfo"
      >
        <el-form-item
          label="名字"
          prop="cli_name"
        >
          <el-input
            type="text"
            v-model="updClientInfo.cli_name"
            style="width: 100%"
          ></el-input>
        </el-form-item>
        <el-form-item label="职务"  prop="cli_job">
          <el-input
            type="text"
            v-model="updClientInfo.cli_job"
            style="width: 100%"
          ></el-input>
        </el-form-item>
        <el-form-item label="电话"  prop="cli_tel">
          <el-input
            type="text"
            v-model="updClientInfo.cli_tel"
            style="width: 100%"
          ></el-input>
        </el-form-item>
        <el-form-item label="性别"  prop="cli_sex">
          <el-select v-model="updClientInfo.cli_sex" placeholder="请选择">
            <el-option label="女" :value="1"></el-option>
            <el-option label="男" :value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="ClientInfoDialog = false">取 消</el-button>
        <el-button type="primary" @click="save('cliForm')">保 存</el-button>
      </div>
    </el-dialog>
    <!-- 修改模态框结束 -->
  </div>
</template>
<script>
export default {
  data() {
    return {
      flag: "",
      checked: "",
      value: "男",
      options: [
        {
          value: "1",
          label: "女",
        },
        {
          value: "2",
          label: "男",
        },
      ],
      keyWord: "",
      ClientList: [],
      companyId: "",
      updClientInfo: {
        cli_id: "",
        com_id: "",
        cli_tel: "",
        cli_name: "",
        cli_sex: "",
        cli_job: "",
        cli_status: "",
      },
      rules: {
        cli_name: [
          { required: true, message: "名字不能为空", trigger: "blur" },
        ],
        cli_job: [{ required: true, message: "职位不能为空", trigger: "blur" }],
        cli_tel: [
          { required: true, message: "电话不能为空", trigger: "blur" },
          { len: 11, message: "电话号码长度不符", trigger: "blur" },
        ],
        cli_sex: [{ required: true, message: "请选择性别", trigger: "change" }],
      },
      ClientInfoDialog: false,
    };
  },
  methods: {
    LoadCusInfo() {
      this.$axios
        .get("/clients/" + this.companyId)
        .then((res) => {
          this.ClientList = res.data.data;
        })
        .catch((error) => {});
    },
    //修改按钮事件
    updCliInfo(cliInfo) {
      this.flag = "1";
      //获得当前用户的信息
      this.$axios
        .get("/clients/onlyOne/" + cliInfo.cli_id)
        .then((res) => {
          this.updClientInfo = res.data.data;
          // this.updCliInfo.cli_sex = res.data.data.cli_sex
          if (res.data.data.cli_sex == "1") {
            this.updCliInfo.cli_sex = "1";
          } else if (res.data.data.cli_sex == "2") {
            this.updCliInfo.cli_sex = "2";
          }

          this.ClientInfoDialog = true;
        })
        .catch((error) => {});
    },
    //保存修改的值
    saveUpdCliInfo(cliForm) {
      this.$refs[cliForm].validate((res) => {
        if (res) {
          // this.updClientInfo.cli_sex = this.value;
          this.$axios
            .put("/clients", this.updClientInfo)
            .then((res) => {
              this.LoadCusInfo();
              this.ClientInfoDialog = false;
              this.$message({
                type: "success",
                message: "修改成功!",
              });
            })
            .catch((error) => {});
        } else {
          console.log("error submit");
          return false;
        }
      });
    },
    //保存新增的值
    saveAddCliInfo(cliForm) {
      this.$refs[cliForm].validate((res) => {
        if (res) {
          // this.updClientInfo.cli_sex = this.value;
          this.$axios
            .post("/clientsAdd", this.updClientInfo)
            .then((res) => {
              if (res.data.status == 200) {
                this.LoadCusInfo();
                this.ClientInfoDialog = false;
                this.$message({
                  type: "success",
                  message: "添加成功!",
                });
              } else {
                this.$message({
                  type: "info",
                  message: "添加失败!",
                });
              }
            })
            .catch((error) => {});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //删除联系人
    delClient(clientInfo) {
      //
      this.$confirm("此操作将删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .post("/clients/del/" + clientInfo.cli_id)
            .then((res) => {
              this.LoadCusInfo();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            })
            .catch((error) => {});
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      //
    },
    //增加联系人按钮事件
    addClient() {
      this.flag = "2";
      this.value = "请选择";
      this.updClientInfo.cli_id = "";
      this.updClientInfo.cli_tel = "";
      this.updClientInfo.cli_name = "";
      this.updClientInfo.cli_sex = "";
      this.updClientInfo.cli_job = "";
      this.updClientInfo.cli_status = "1";
      this.updClientInfo.com_id = this.companyId;
      this.ClientInfoDialog = true;
    },
    //查询
    search() {
      this.$axios
        .get("/clientsSearch", {
          params: { name: this.keyWord, com_id: this.companyId },
        })
        .then((res) => {
          this.ClientList = res.data.data;
        })
        .catch((error) => {});
    },
    //保存模态框中的数据，flag为1的是保存修改，flag为2的是保存新增。
    save(cliForm) {
      if (this.flag == "1") {
        this.saveUpdCliInfo(cliForm);
      } else if (this.flag == "2") {
        this.saveAddCliInfo(cliForm);
      }
    },
  },
  mounted() {
    this.companyId = this.$route.params.id;
    this.LoadCusInfo();
  },
};
</script>